package com.jeeplus.modules.sys.service;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.BaseService;
import com.jeeplus.modules.sys.entity.*;
import com.jeeplus.modules.sys.mapper.SysHomeTongJiMapper;
import com.jeeplus.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author xujin
 * @title: SysHomeTongJiService
 * @projectName ESA
 * @description: TODO
 * @date 2020-01-159:24
 */
@Service
@Transactional(readOnly = true)
public class SysHomeTongJiService extends BaseService {

    @Autowired
    SysHomeTongJiMapper sysHomeTongJiMapper;

    public LinkedHashMap getSellData(){
        LinkedHashMap resultMap = new LinkedHashMap();
        int year =0;
        List yearList = new ArrayList<>();
        List data1 = new ArrayList<>();
        List data2 = new ArrayList<>();
        List data3 = new ArrayList<>();
        try {
            List<HashMap> dataList = sysHomeTongJiMapper.getSellData();
            for(HashMap data : dataList){
                int year1 = (int)data.get("fYear");
                if(year ==0 || year != year1){
                    year = year1;
                    yearList.add(year+"年");
                }
                if(yearList.size() == 1){
                    data1.add(data.get("fSum"));
                }
                if(yearList.size() == 2){
                    data2.add(data.get("fSum"));
                }
                if(yearList.size() == 3){
                    data3.add(data.get("fSum"));
                }

            }
            resultMap.put("yearList",yearList);
            resultMap.put("data1",data1);
            resultMap.put("data2",data2);
            resultMap.put("data3",data3);

        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return resultMap;
    }

    public LinkedHashMap getSellUnitPerData(){
        LinkedHashMap resultMap = new LinkedHashMap();
        int count = 0;//总数量
        List sellUnitList = new ArrayList<>();//销售归属列表
        Map selectedMap = new HashMap<>();//是否选中map
        try {
            List<HashMap> dataList = sysHomeTongJiMapper.getSellUnitPerData();
            for(Map data : dataList){
                count = count + (int)data.get("value");
                sellUnitList.add(data.get("name"));
                selectedMap.put(data.get("name"),true);
            }
            resultMap.put("count",count);
            resultMap.put("sellUnitList",sellUnitList);
            resultMap.put("dataList",dataList);
            resultMap.put("selectedMap",selectedMap);

        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return resultMap;
    }

    public LinkedHashMap getRiskSellData(){
        LinkedHashMap resultMap = new LinkedHashMap();
        HashMap dataMap = new HashMap();
        String xsgsName = "";
        List legendDataList = new ArrayList<>();
        List dataX = new ArrayList<>();
        try {
            List<HashMap> dataList = sysHomeTongJiMapper.getRiskSellData();
            for(HashMap data : dataList){
                String xsgsName1 = (String)data.get("xsgsname");
                if("".equals(xsgsName) || !xsgsName.equals(xsgsName1)){//取出销售归属的名字
                    xsgsName = xsgsName1;
                    legendDataList.add(xsgsName);
                }else {
                    continue;
                }
            }
            for(int i = 0; i<legendDataList.size(); i++){//根据销售归属的名字取出对应的统计数据
                List data_tj = new ArrayList<>();
                for(HashMap data : dataList){
                    if(legendDataList.get(i).equals(data.get("xsgsname"))){
                        data_tj.add(data.get("ts"));
                        if(i==0){
                            dataX.add(data.get("fxxs"));//只获取一次横坐标的值
                        }
                    }else {
                        continue;
                    }
                }
                dataMap.put(legendDataList.get(i),data_tj);
            }
            resultMap.put("legendDataList",legendDataList);
            resultMap.put("dataX",dataX);
            resultMap.put("dataMap",dataMap);

        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return resultMap;
    }

    public LinkedHashMap getRiskArrearsData(){
        LinkedHashMap resultMap = new LinkedHashMap();
        HashMap dataMap = new HashMap();
        String xsgsName = "";
        List legendDataList = new ArrayList<>();
        List dataX = new ArrayList<>();
        try {
            List<HashMap> dataList = sysHomeTongJiMapper.getRiskArrearsData();
            for(HashMap data : dataList){
                String xsgsName1 = (String)data.get("xsgsname");
                if("".equals(xsgsName) || !xsgsName.equals(xsgsName1)){//取出销售归属的名字
                    xsgsName = xsgsName1;
                    legendDataList.add(xsgsName);
                }else {
                    continue;
                }
            }
            for(int i = 0; i<legendDataList.size(); i++){//根据销售归属的名字取出对应的统计数据
                List data_tj = new ArrayList<>();
                for(HashMap data : dataList){
                    if(legendDataList.get(i).equals(data.get("xsgsname"))){
                        data_tj.add(data.get("corpus"));
                        if(i==0){
                            dataX.add(data.get("fxxs"));//只获取一次横坐标的值
                        }
                    }else {
                        continue;
                    }
                }
                dataMap.put(legendDataList.get(i),data_tj);
            }
            resultMap.put("legendDataList",legendDataList);
            resultMap.put("dataX",dataX);
            resultMap.put("dataMap",dataMap);

        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        return resultMap;
    }

    public LinkedHashMap getHomePie(String user){
        LinkedHashMap resultMap = new LinkedHashMap();
        if("".equals(user)){
            user = UserUtils.getUser().getId();
        }
        List<HashMap> ccDataList= new ArrayList<>();
        List<HashMap> qyDataList= new ArrayList<>();
        List<HashMap> dqDataList= new ArrayList<>();
        List<HashMap> cxDataList= new ArrayList<>();
        List<HashMap> jrDataList= new ArrayList<>();
        List<HashMap> ypDataList= new ArrayList<>();
        List<HashMap> jrzbDataList= new ArrayList<>();
        List<HashMap> yswhfsDataList= new ArrayList<>();
        List<HashMap> yswkhDataList= new ArrayList<>();
        List<HashMap> qt1DataList= new ArrayList<>();
        List<HashMap> qt2DataList= new ArrayList<>();
        List<HashMap> mapList = sysHomeTongJiMapper.getHomePie(user);
        for(HashMap map : mapList){
            String type = (String) map.get("type");
            if("库存统计".equals(type)){
                ccDataList.add(map);
                continue;
            }
            if("区域销售".equals(type)){
                qyDataList.add(map);
                continue;
            }
            if("贷前进度".equals(type)){
                dqDataList.add(map);
                continue;
            }
            if("车型销售".equals(type)){
                cxDataList.add(map);
                continue;
            }
            if("金融占比".equals(type)){
                jrDataList.add(map);
                continue;
            }
            if("预排资源".equals(type)){
                ypDataList.add(map);
                continue;
            }
            if("金融".equals(type)){
                jrzbDataList.add(map);
                continue;
            }
            if("应收未回销售方式".equals(type)){
                yswhfsDataList.add(map);
                continue;
            }
            if("已售无客户".equals(type)){
                yswkhDataList.add(map);
                continue;
            }
            if("其他1".equals(type)){
                qt1DataList.add(map);
                continue;
            }
            if("qt2".equals(type)){
                qt2DataList.add(map);
                continue;
            }
        }

        resultMap.put("ccData", init(ccDataList));
        resultMap.put("qyData", init(qyDataList));
        resultMap.put("dqData", init(dqDataList));
        resultMap.put("cxData", init(cxDataList));
        resultMap.put("jrData", init(jrDataList));
        resultMap.put("ypData", init(ypDataList));
        resultMap.put("jrzbData", init(jrzbDataList));
        resultMap.put("yswhfsData", init(yswhfsDataList));
        resultMap.put("yswkhData", init(yswkhDataList));
        resultMap.put("qt1Data", init(qt1DataList));
        resultMap.put("qt2Data", init(qt2DataList));
        return resultMap;
    }

    public Map findTongTjByCompId(String company){
        if("".equals(company)){
            company = UserUtils.getUser().getCompany().getId();
        }
        return sysHomeTongJiMapper.findTongTjByCompId(company);
    }

    public HashMap init(List<HashMap> dataList){
        List legendDataList = new ArrayList<>();
        List selectDataList = new ArrayList<>();
        HashMap resMap = new HashMap();
        int count = 0;
        for(HashMap data : dataList){
            Map map = new HashMap();
            String name = (String)data.get("name");
            map.put(name,true);
            legendDataList.add(name);
            selectDataList.add(map);
            count = count + (int)data.get("value");
        }

        resMap.put("dataList",dataList);
        resMap.put("legendDataList",legendDataList);
        resMap.put("selectDataList",selectDataList);
        resMap.put("count",count);

        return resMap;
    }

    public Page<CgDetail> findCgPage(CgDetail cgDetail, Page<CgDetail> page) {
        dataRuleFilter(cgDetail);
        cgDetail.setPage(page);
        page.setList(sysHomeTongJiMapper.cgDataDetail(cgDetail));
        return page;
    }

    public Page<CkDetail> findCkPage(CkDetail ckDetail, Page<CkDetail> page) {
        dataRuleFilter(ckDetail);
        ckDetail.setPage(page);
        page.setList(sysHomeTongJiMapper.ckDataDetail(ckDetail));
        return page;
    }



    public Page<DdDetail> findDdPage(DdDetail ddDetail, Page<DdDetail> page) {
        dataRuleFilter(ddDetail);
        ddDetail.setPage(page);
        page.setList(sysHomeTongJiMapper.ddDataDetail(ddDetail));
        return page;
    }

    public Page<XxDetail> findDdPage(XxDetail xxDetail, Page<XxDetail> page) {
        dataRuleFilter(xxDetail);
        xxDetail.setPage(page);
        page.setList(sysHomeTongJiMapper.xxDataDetail(xxDetail));
        return page;
    }



    public Page<ZkkcDetail> findZkkcPage(ZkkcDetail zkkcDetail, Page<ZkkcDetail> page) {
        dataRuleFilter(zkkcDetail);
        zkkcDetail.setPage(page);
        page.setList(sysHomeTongJiMapper.zkkcDataDetail(zkkcDetail));
        return page;
    }

    /*库存统计-期初*/
    public Page<CkqcDetail> findckqcPage(CkqcDetail ckqcDetail, Page<CkqcDetail> page) {
        dataRuleFilter(ckqcDetail);
        ckqcDetail.setPage(page);
        page.setList(sysHomeTongJiMapper.ckqcDataDetail(ckqcDetail));
        return page;
    }

    /*库存统计-当期*/
    public Page<KcdqDetail> findkcdqPage(KcdqDetail kcdqDetail, Page<KcdqDetail> page) {
        dataRuleFilter(kcdqDetail);
        kcdqDetail.setPage(page);
        page.setList(sysHomeTongJiMapper.kcdqDataDetail(kcdqDetail));
        return page;
    }

    /*库存统计-超期*/
    public Page<KccqDetail> findkccqPage(KccqDetail kccqDetail, Page<KccqDetail> page) {
        dataRuleFilter(kccqDetail);
        kccqDetail.setPage(page);
        page.setList(sysHomeTongJiMapper.kccqDataDetail(kccqDetail));
        return page;
    }


    public Page<QyxsDetail> findQyxsPage(QyxsDetail qyxsDetail, Page<QyxsDetail> page) {
        dataRuleFilter(qyxsDetail);
        qyxsDetail.setPage(page);
        page.setList(sysHomeTongJiMapper.qyxsDataDetail(qyxsDetail));
        return page;
    }

    public Page<DqjdDetail> findDqjdPage(DqjdDetail dqjdDetail, Page<DqjdDetail> page) {
        dataRuleFilter(dqjdDetail);
        dqjdDetail.setPage(page);
        page.setList(sysHomeTongJiMapper.dqjdDataDetail(dqjdDetail));
        return page;
    }

    public Page<CxDetail> findCxPage(CxDetail cxDetail, Page<CxDetail> page) {
        dataRuleFilter(cxDetail);
        cxDetail.setPage(page);
        page.setList(sysHomeTongJiMapper.cxDataDetail(cxDetail));
        return page;
    }

    public Page<JrcxDetail> findJrcxPage(JrcxDetail jrcxDetail, Page<JrcxDetail> page) {
        dataRuleFilter(jrcxDetail);
        jrcxDetail.setPage(page);
        page.setList(sysHomeTongJiMapper.jrcxDataDetail(jrcxDetail));
        return page;
    }


    public Page<GgzyDetail> findGgzyPage(GgzyDetail ggzyDetail, Page<GgzyDetail> page) {
        dataRuleFilter(ggzyDetail);
        ggzyDetail.setPage(page);
        page.setList(sysHomeTongJiMapper.ggzyDataDetail(ggzyDetail));
        return page;
    }


    public Page<JrzbDetail> findJrzbPage(JrzbDetail jrzbDetail, Page<JrzbDetail> page) {
        dataRuleFilter(jrzbDetail);
        jrzbDetail.setPage(page);
        page.setList(sysHomeTongJiMapper.jrzbDataDetail(jrzbDetail));
        return page;
    }

    public Page<YswhfsDetail> findYswhfsPage(YswhfsDetail yswhfsDetail, Page<YswhfsDetail> page) {
        dataRuleFilter(yswhfsDetail);
        yswhfsDetail.setPage(page);
        page.setList(sysHomeTongJiMapper.yswhfsDataDetail(yswhfsDetail));
        return page;
    }

    public Page<YswkhDetail> findYswkhPage(YswkhDetail yswkhDetail, Page<YswkhDetail> page) {
        dataRuleFilter(yswkhDetail);
        yswkhDetail.setPage(page);
        page.setList(sysHomeTongJiMapper.yswkhDataDetail(yswkhDetail));
        return page;
    }

    public Page<QktjDetail> findQktjPage(QktjDetail qktjDetail, Page<QktjDetail> page) {
        dataRuleFilter(qktjDetail);
        qktjDetail.setPage(page);
        page.setList(sysHomeTongJiMapper.qktjDataDetail(qktjDetail));
        return page;
    }


}
